<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: ThemeMan.php 425 2011-01-05 10:30:30Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalThemeRecord');
Prado::using('Application.Common.Data.PortalThemeCssClassRecord');
Prado::using('Application.Common.Data.PortalThemeCssClassAttrRecord');
Prado::using('Application.Common.PortalParseCss');

class ThemeMan extends PortalAdminPage
{
	public function getDataRows($offset, $rows)
	{
		return $this->DataAccess->createCommand("select * from tbltheme order by name LIMIT $offset,$rows")->query();
	}

	protected function getRowCount()
    {
        return $this->DataAccess->createCommand("select count(id) from tbltheme")->queryScalar();
    }

	public function bindData($offset)
	{
		$this->ThemeList->VirtualItemCount	= $this->RowCount;
		$this->ThemeList->DataSource		= $this->getDataRows($offset,$this->ThemeList->PageSize);
		$this->ThemeList->dataBind();
	}

	public function pageChanged($sender,$param)
    {
        $this->ThemeList->CurrentPageIndex	= $param->NewPageIndex;
        $this->bindData($this->ThemeList->CurrentPageIndex * $this->ThemeList->PageSize);
    }

	public function onLoad($param)
	{
		parent::onLoad($param);

		if(!$this->IsPostBack)
		{
			$this->bindData(0);
		}
	}

	public function itemCommand($s,$p)
	{
		switch($p->CommandName)
		{
			case "importcss": $this->ImportCss($p->CommandParameter); break;
			case "getsystemcss": $this->SystemCss($p->CommandParameter); break;
			case "savecss": $this->SaveCss($p->CommandParameter); break;
		}

		$this->bindData(0);
	}

	public function deleteItem($sender, $param)
	{
		$id			= $this->ThemeList->DataKeys[$param->Item->ItemIndex];
		$themeRec	= PortalThemeRecord::finder()->findByid($id);

		if($themeRec !== null)
		{
			$themeRec->delete();
		}

		$this->bindData(0);
	}

	public function getClassCount($themeid)
	{
		 return $this->DataAccess->createCommand("select count(id) from tblthemecssclasses where themeid=$themeid")->queryScalar();
	}
/*
	public function ImportCss($id)
	{
		$themeRec	= PortalThemeRecord::finder()->findByid($id);
		$themepath	= PortalUtil::pathName($this->Application->PortalBasePath . '/themes/Portal/' . $themeRec->name);
		$cssfiles	= $this->getCssFilesInPath($themepath);

		if(is_array($cssfiles) && count($cssfiles) > 0)
		{
			foreach($cssfiles as $cssfile)
			{
				$cssParse	= new PortalParseCss();
				$cssClasses	= $cssParse->parseFile($themepath . $cssfile);

				if(is_array($cssClasses) && count($cssClasses) > 0)
				{
					foreach($cssClasses as $key => $cssClass)
					{
						$cssClassRec	= PortalThemeCssClassRecord::finder()->findBythemeid_AND_name($id,$key);

						if($cssClassRec === null)
						{
							$cssClassRec	= new PortalThemeCssClassRecord();
							$cssClassRec->themeid	= $id;
							$cssClassRec->name		= trim($key);
							$cssClassRec->active	= 1;
							$cssClassRec->save();
						}

						// Save attributes
						if(is_array($cssClass) && count($cssClass) > 0)
						{
							foreach($cssClass as $aKey => $aValue)
							{
								$cssAttrRec	= PortalThemeCssClassAttrRecord::finder()->findBytcid_AND_name($cssClassRec->id,$aKey);

								if($cssAttrRec === null)
								{
									$cssAttrRec	= new PortalThemeCssClassAttrRecord();
									$cssAttrRec->tcid	= $cssClassRec->id;
									$cssAttrRec->name	= trim($aKey);
								}

								$cssAttrRec->attr	= trim($aValue);
								$cssAttrRec->save();
							}
						}
					}
				}
			}
		}

		$this->bindData(0);
	}
*/
	/*
	public function getCssFilesInPath($path)
	{
		$basePath		= $path;
		$cssfiles 		= array();

		if(!is_dir($basePath))
		{
			return $cssfiles;
		}

		$folder = @opendir($basePath);
		while($file = @readdir($folder))
		{
			if($file!=='.' && $file!=='..' && $file!=='.svn' && !is_dir($basePath.'/'.$file))
			{
				$path_parts = pathinfo($file);

				if($path_parts['extension'] == "css")
				{
					$cssfiles[] = $file;
				}
			}
		}

		closedir($folder);
		return $cssfiles;
	}
	*/

	public function SystemCss($themeid)
	{
		$appl_css		= $this->Application->getApplicationCss();
		$portlet_css	= PortalUtil::GetCssClassesInAllPortlets();
		$all_css		= array_merge($appl_css,$portlet_css);

		if(is_array($all_css) && count($all_css) > 0)
		{
			foreach($all_css as $key => $values)
			{
				$cssClass	= PortalThemeCssClassRecord::finder()->findBythemeid_AND_name($themeid, $key);

				if($cssClass === null)
				{
					$cssClass = new PortalThemeCssClassRecord();
					$cssClass->themeid	= $themeid;
					$cssClass->name		= trim($key);
					$cssClass->active	= true;
					$cssClass->save();
				}

				if(is_array($values) && count($values) > 0)
				{
					foreach($values as $aKey => $aValue)
					{
						$cssAttr	= PortalThemeCssClassAttrRecord::finder()->findBytcid_AND_name($cssClass->id,$aKey);

						if($cssAttr === null)
						{
							$cssAttr = new PortalThemeCssClassAttrRecord();
							$cssAttr->tcid	= $cssClass->id;
							$cssAttr->name	= trim($aKey);
						}

						$cssAttr->attr	= trim($aValue);
						$cssAttr->save();
					}
				}
				else if(is_string($values) && !empty($values))
				{
					$cssAttr	= PortalThemeCssClassAttrRecord::finder()->findBytcid_AND_name($cssClass->id,$key);

					if($cssAttr === null)
					{
						$cssAttr = new PortalThemeCssClassAttrRecord();
						$cssAttr->tcid	= $cssClass->id;
						$cssAttr->name	= trim($key);
					}

					$cssAttr->attr	= trim($values);
					$cssAttr->save();
				}
			}
		}
	}

	public function SaveCss($id)
	{
		$filecontent		= "";
		$themeRec			= PortalThemeRecord::finder()->findByid($id);
		$themepath			= PortalUtil::pathName($this->Application->PortalBasePath . '/themes/Portal/' . $themeRec->name);
		$themeCssFileName	= $themepath . '/style.css'; // Are we sure that this is the correct name ?
		$themeCssClasses	= PortalThemeCssClassRecord::finder()->findAllBythemeid($id);

		if($themeCssClasses)
		{
			foreach($themeCssClasses as $themeCssClass)
			{
				$filecontent .= $themeCssClass->name . "\r\n";
				$filecontent .= "{\r\n";
				$tcAttrs	= PortalThemeCssClassAttrRecord::finder()->findAllBytcid($themeCssClass->id);

				if($tcAttrs)
				{
					foreach($tcAttrs as $tcAttr)
					{
						$filecontent .= "    " .$tcAttr->name . ': ' . $tcAttr->attr .";\r\n";
					}
				}
				$filecontent .= "}\r\n\r\n";
			}
		}
		try
		{
			file_put_contents($themeCssFileName,$filecontent);
		}
		catch (Exception $e)
		{
			
		}
		
	}
}
?>